package planeGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

/**
 * 飞机游戏的主窗口
 */
public class MyGameFrame extends Frame{
    Image bg = GameUtil.getImage("images/bg.jpg");
    Image planeImg = GameUtil.getImage("images/plane.png");

    //加载飞机类
    Plane plane = new Plane(planeImg,250,250);
    //炮弹类
    Shell[] shells = new Shell[50];
    //声明爆炸类
    Explode bao;
    //游戏开始时间
    Date startTime = new Date();
    //结束时间
    Date endTime;
    int period;//游戏持续时间
    @Override
    public void paint(Graphics g) {//自动被调用，g相当于一支画笔
        super.paint(g);
        Color c = g.getColor();
        g.drawImage(bg,0,0,null);
        plane.drowSelf(g);//飞机类下的加载图片
        //画炮弹
        for(int i=0;i<shells.length;i++){
            shells[i].draw(g);
            //碰撞检测
            if(shells[i].getRect().intersects(plane.getRect())) {//获取子弹的矩形和飞机的矩形判断是否相交
                plane.live=false;
                if(bao==null){//子弹接触之后一直处于碰撞状态，始终new一个最小的图片出来,只new一次就没有问题了
                    bao = new Explode(plane.x,plane.y);
                }
                bao.draw(g);
                //结束时间
                endTime = new Date();
                period = (int)((endTime.getTime()-startTime.getTime())/1000);
            }
            if(!plane.live){
                g.setColor(Color.white);
                Font f = new Font("宋体",Font.BOLD,50);
                g.setFont(f);
                g.drawString("时间"+period+"秒",(int)plane.x,(int)plane.y);
            }
        }
        g.setColor(c);
    }

    //反复重画窗口线程
    class PaintThread extends Thread{
        @Override
        public void run() {
            while (true){
                repaint();//重画
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //键盘监听
    class keyMonitor extends KeyAdapter{
        //重写父类的两个方法
        @Override
        public void keyPressed(KeyEvent e) {//摁下
           plane.addDirection(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {//抬起
            plane.minusDirection(e);
        }
    }

    //初始化窗口
    public void launchFrame(){
        this.setTitle("飞机大战");
        this.setVisible(true);//窗口可见
        this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
        this.setLocation(380,120);//距离屏幕左上角的距离;

        this.addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();//启动重画窗口的线程
        addKeyListener(new keyMonitor());

        //初始化n个炮弹
        for (int i=0;i<shells.length;i++){
            shells[i]=new Shell();
        }
    }

    public static void main(String[] args){
        MyGameFrame f = new MyGameFrame();
        f.launchFrame();
    }

    //双缓冲技术不让屏幕闪烁
    private Image offScreenImage = null;

    public void update(Graphics g) {
        if(offScreenImage == null)
            offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度

        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }
}
